package com.bblocks.common.anti_reptile.module;

import com.bblocks.common.anti_reptile.constant.RuleResultEnum;
import com.bblocks.common.anti_reptile.constant.RuleTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * <p>Project: bb-anti-reptile - RuleVO</p>
 * <p>描述：结果访问规则</p>
 * rateAvg 、 rateMax
 * 原则：
 * 1、rateAvg 、 rateMax  值一致。但是rateMax=rateAvg*multiplier/100.0
 * 2、原则上流量限制用 rateMax，
 * @Author Zhao [125043150@qq.com]
 * @Date 2024/3/23 15:22
 * @Version 1.0
 * @since 8
 */
//@Builder
@AllArgsConstructor
@NoArgsConstructor
@Data
public class RuleVO implements Serializable {
    /**
     * 规则类型：3=ip，4=user
     */
    private int type = 3;

    /**
     * 规则规则地址
     */
    private String url;

    /**
     * 平均qps(等于0的按1算)
     */
    private Integer rateAvg;

    /**
     * 最大qps(原则用此值限速)(等于0的按1算)，目前此值已经乘以 multiplier/100.0 了
     */
    private Integer rateMax;

    /**
     * 处理结果
     */
    private RuleResultEnum ruleResultEnum;


    public static RuleVOBuilder builder() {
        return new RuleVOBuilder();
    }

    public static class RuleVOBuilder {
        private final RuleVO ruleVO = new RuleVO();

        public RuleVOBuilder url(String url){
            ruleVO.setUrl(url);
            return this;
        }

        public RuleVOBuilder type(int type){
            ruleVO.setType(type);
            return this;
        }

        /**
         * 设置均值
         * 如果<=0，则标记为1
         * @param rateAvg
         * @return
         */
        public RuleVOBuilder rateAvg(Integer rateAvg){
            if(rateAvg <= 0){
                rateAvg = 1;
            }
            ruleVO.setRateAvg(rateAvg);
            return this;
        }

        /**
         * 设置最大值
         * 如果<=0，则标记为1
         * @param rateMax
         * @return
         */
        public RuleVOBuilder rateMax(Integer rateMax){
            if(rateMax <= 0){
                rateMax = 1;
            }
            ruleVO.setRateMax(rateMax);
            return this;
        }

        public RuleVOBuilder ruleResultEnum(RuleResultEnum ruleResultEnum){
            ruleVO.setRuleResultEnum(ruleResultEnum);
            return this;
        }

        public RuleVO build(){
            return this.ruleVO;
        }
    }
}
